<template>
<div>
  <div class="chat-content" ref="chatContentRef">
    <div style="margin-left: 47%;margin-top: 2%;color: #7eaae8">与{{ usersFriend.users.usersName }}的聊天室</div>
    <div v-for="item in chatContentData" class="chat-array">
      <div class="word-my" v-if="item.substring(item.lastIndexOf('/')+1) === JSON.stringify(users.usersId)">
        <div class="info">
          <p class="time">
            {{users.usersName}}
          </p>
          <div class="info-content">
            {{ item.substring(0,item.lastIndexOf("/")) }}
          </div>
        </div>
        <el-image :src="users.usersPortrait" />
      </div>
      <div class="word" v-else>
        <el-image :src="usersFriend.users.usersPortrait" />
        <div class="info">
          <p class="time">
            {{usersFriend.users.usersName}}
          </p>
          <div class="info-content">
            {{ item.substring(0,item.lastIndexOf("/")) }}
          </div>
        </div>
      </div>
    </div>
  </div>
  <div v-if="chatContentData.length !== 0" class="chat-user-operation">
    <el-input v-model="chatData.newMessage" class="chat-input" :placeholder="inputPlaceholder" />
    <el-button type="primary" plain @click="issueContent()" class="chat-submit">发布</el-button>
  </div>
</div>
</template>

<script>
import request from "../../../utils/request";
import newVue from '../../../utils/newVue'
export default {
  name: "chatContent",
  data(){
    return{
      users:{},
      usersFriend:{
        users:{
          usersId:0,
        }
      },
      chatContentData:[],
      forbidInputState:true,
      inputPlaceholder:'请输入内容',
      chatData:{
        chatUser:0,
        chatFiend:0,
        chatKey:'',
        newMessage:''
      },
    }
  },
  methods:{
    issueContent(){
      if (this.chatData.newMessage !== '' && this.forbidInputState === true){
        this.chatData.chatUser=this.users.usersId
        this.chatData.chatFriend=this.usersFriend.users.usersId
        this.chatData.newMessage=this.chatData.newMessage+'/'+this.users.usersId
        request.post('/chat/inTheChat',this.chatData).then(
            this.timing
        )
        this.chatContentData=this.chatContentData.concat(this.chatData.newMessage)
        this.chatData.newMessage=''
        return
      }
      this.$message.warning("请检查下内容并且稍等片刻~")
    },
    timing(){
      this.forbidInputState=false
      let num=5;
      let times = setInterval(() => {
        this.inputPlaceholder=(num--)+'秒后再提交'
        if (num <= 0) {
          this.forbidInputState = true;
          this.inputPlaceholder="请输入内容"
          clearInterval(times);
        }
      }, 1000);
    },
    inChatContent(){
      request.get("/chat/getChatContent/"+this.chatData.chatKey+"/"+this.usersFriend.users.usersId).then(res=>{
        this.chatContentData=res.chatContent
        this.usersFriend.users=res.byUser
        this.$refs.chatContentRef.scrollTop = this.$refs.chatContentRef.scrollHeight;
      })
    },
  },
  created() {
    this.users=JSON.parse(localStorage.getItem("userData"))
  },
  watch:{
    '$route.path': {
      handler(){
        if (this.$route.params.key){
          this.chatData.chatKey=this.$route.params.key
          this.usersFriend.users.usersId=this.$route.params.usersId
          this.inChatContent()
        }
      },
      deep: true, // 深度监听
      immediate: true, // 第一次初始化渲染就可以监听到
    }
  },
  mounted() {
    this.inChatContent();
    newVue.$on('newMessage', val => {
      this.chatContentData=this.chatContentData.concat(val)
      this.$refs.chatContentRef.scrollTop = this.$refs.chatContentRef.scrollHeight;
    })
  }
}
</script>

<style lang="scss">
.chat-user-operation{
  width: 100%;
.chat-input{
  width: 70%;
  display: inline-block
}

.chat-submit{
  width: 29%;
  float: right
}
}

.chat-content {
  width: 100%;
  height: 600px;
  overflow: scroll;

.chat-array {
  margin:2%;
.word {
  display: flex;
  margin-bottom: 20px;

img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.info {
  margin-left: 10px;

.time {
  font-size: 12px;
  color: rgba(51, 51, 51, 0.8);
  margin: 0;
  height: 20px;
  line-height: 20px;
  margin-top: -5px;
}

.info-content {
  padding: 10px;
  font-size: 14px;
  background: #A3C3F6;
  position: relative;
  margin-top: 8px;
}

//小三角形
  .info-content::before {
    position: absolute;
    left: -8px;
    top: 8px;
    content: '';
    border-right: 10px solid #A3C3F6;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
  }
}
}

.word-my {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;

img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.info {
  width: 90%;
  margin-left: 10px;
  text-align: right;

.time {
  font-size: 12px;
  color: rgba(51, 51, 51, 0.8);
  margin: 0;
  height: 20px;
  line-height: 20px;
  margin-top: -5px;
  margin-right: 10px;
}

.info-content {
  max-width: 70%;
  padding: 10px;
  font-size: 14px;
  float: right;
  margin-right: 10px;
  position: relative;
  margin-top: 8px;
  background: #A3C3F6;
  text-align: left;
}

//小三角形
  .info-content::after {
    position: absolute;
    right: -8px;
    top: 8px;
    content: '';
    border-left: 10px solid #A3C3F6;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
  }
}
}
}
}
</style>